package miko.mikoplex.view;

import java.util.HashMap;
import java.util.logging.Logger;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;

import miko.mikoplex.model.GameBoardModel;
import miko.mikoplex.utils.PairInt;

public class GameBoardView {
	public final GameBoardModel gb;
	public HashMap<PairInt, Geometry> tileHash = new HashMap<PairInt, Geometry>();
	public final Node gameBoardNode;
	public AssetManager assetManager;
	public GameBoardView(String name,GameBoardModel _gb,AssetManager am) {
		assetManager = am;
		gameBoardNode = new Node(name);
		Vector3f start = new Vector3f(0.0f,0.0f,0.0f);
		float tileSize = 10.0f,tileSpace = 0.2f,tileLength = tileSize+tileSpace;
		gb = _gb;
		
		Logger.getLogger(this.getClass().getName()).info("CWD: "+System.getProperty("user.dir"));
		
		Material mat = new Material(assetManager,"Common/MatDefs/Misc/SimpleTextured.j3md");
		mat.setTexture("m_ColorMap", assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg"));
		Material smiley = new Material(assetManager,"Common/MatDefs/Misc/SimpleTextured.j3md");
		smiley.setTexture("m_ColorMap", assetManager.loadTexture("img/smiley.png"));
//		mat.setColor("m_Color", ColorRGBA.Blue);
		
		for (int i=0;i<gb.width;i++) for (int j=0;j<gb.height;j++) {
			Box tileBox = new Box(new Vector3f(tileLength*i,tileLength*j,0f),
					tileSize/2f,tileSize/2f,0f);
			Geometry tile = new Geometry("gb_"+name+i+"_"+j,tileBox);
			
			if (gb.get(i, j) == ' ') tile.setMaterial(smiley);
			else tile.setMaterial(mat);
			
			gameBoardNode.attachChild(tile);
			tileHash.put(new PairInt(i,j),tile);
			if (Character.isWhitespace(gb.get(i, j))) continue;
		}
	}
}
